package com.mark.tim.kutspel;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;

public class MainActivity extends Activity {

	private static final String TAG = "MainActivity";
	private PlayView playView;
	private Plane plane;
	static boolean debug = false;
	static boolean changed = false;
	static boolean ronald = false;
	public static boolean D = true;

	private SensorManager sensorManager;
	private SensorEventListener eventListener;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);

		playView = (PlayView) findViewById(R.id.playView1);
		plane = new Plane();
		playView.setPlane(plane);
	}

	@Override
	protected void onPause() {
		super.onPause();
		sensorManager.unregisterListener(eventListener);
		playView.stopTimer();
	}

	@Override
	protected void onResume() {
		super.onResume();
		eventListener = new SensorEventListener() {

			@Override
			public void onSensorChanged(SensorEvent event) {
				if(D)Log.d(TAG, Float.toString(event.values[0]));
				playView.setSpeed((int) (event.values[0] * plane.getSpeed()));
			}

			@Override
			public void onAccuracyChanged(Sensor arg0, int arg1) {
			}
		};
		sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		sensorManager.registerListener(eventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_FASTEST);
		playView.startTimer();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public void onBackPressed() {
		playView.stopTimer();
	    new AlertDialog.Builder(this)
        .setTitle("Game Paused")
        .setOnCancelListener(new OnCancelListener() {
			@Override
			public void onCancel(DialogInterface arg0) {
				playView.startTimer();
			}
		}).setItems(R.array.dialogItems, new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case 0 : playView.startTimer();
						 break;
				case 1 : playView.resetValues();
						 playView.startTimer();
						 break;
				case 2 : MainActivity.super.onBackPressed();
						 break;
				}
			}
		}).create().show();
	}
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case R.id.action_upgrade:
			if(playView.getBulletLevel() == 0){
				playView.setBulletLevel(1);
			} else if(playView.getBulletLevel() == 1){
				playView.setBulletLevel(0);
			}
			return true;
		case R.id.action_bullets:
			if(playView.getDelay() == 0){
				playView.setDelay(10);
			} else if(playView.getDelay() == 10){
				playView.setDelay(0);
			}
			return true;
		case R.id.action_debug:
			debug = !debug;
			changed = true;
			return true;
		case R.id.action_ronald:
			ronald = !ronald;
			changed = true;
			return true;
		}
		return false;
	}
}
